package com.jhonny.kiloboltgame.framework;

import java.awt.Image;
import java.util.ArrayList;


public class Animation {
	
	private ArrayList<AnimFrame> frames;
	private int currentFrame;
	private long animTime; //long takes up more memory than int but can hold more accurate numbers.
	private long totalDuration;
	
	public Animation() {
		frames = new ArrayList<AnimFrame>();
		totalDuration = 0;

		synchronized (this) {
			animTime = 0;
			currentFrame = 0;
		}
	}
	
	public synchronized void addFrame(Image image, long duration) {
		totalDuration += duration;
		frames.add(new AnimFrame(image, totalDuration));
	}
	
	public synchronized void update(long elapsedTime) {
		if(frames.size() > 1) {
			animTime += elapsedTime;
			if(animTime >= totalDuration) {
				animTime = animTime % totalDuration;
				currentFrame = 0;
			}

			while(animTime > getFrame(currentFrame).endTime) {
				currentFrame++;
			}
		}
	}
	
	public synchronized Image getImage() {
		if(frames.size() == 0) {
			return null;
		}else{
			return getFrame(currentFrame).image;
		}
	}
	
	private AnimFrame getFrame(int i) {
		return (AnimFrame)frames.get(i);
	}
}
